﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Team4sCMDB
{
    public partial class Main : Form
    {
        private Model model;
        private Panel attributesPanel;

        public Main()
        {
            InitializeComponent();
            loadStyleSearchBox();
            Locations();
            this.Text = "Team4S CMDB - Speed, South, Student, Smart";

            attributesPanel = new Panel();
            attributesPanel.Size = new Size(500, 500);
            attributesPanel.Location = new Point(300, 30);
            this.Controls.Add(attributesPanel);

            this.model = new Model(@"C:\Users\Kevin\Desktop\Team4sCmdb.mdb");

            //fill categories listbox
            foreach (CiCategory category in Enum.GetValues(typeof(CiCategory)))
            {
                lbCategories.Items.Add(category);
            }
        }

        // Static info
        private static string toolStripSearchBox_standardText = "Zoek op CI";

        #region SearchBox Text and Font changes

        private void loadStyleSearchBox()
        {
            toolStripSearchBox.Text = toolStripSearchBox_standardText;
            toolStripSearchBox.ForeColor = SystemColors.ControlDark;
            toolStripSearchBox.Font = new Font(this.Font, FontStyle.Italic);
        }

        private void toolStripSearchBox_Enter(object sender, EventArgs e)
        {
            if ((toolStripSearchBox.Text != toolStripSearchBox_standardText) && (toolStripSearchBox.Text != ""))
            {
                toolStripSearchBox.Text = toolStripSearchBox.Text;
                toolStripSearchBox.ForeColor = SystemColors.ControlText;
                toolStripSearchBox.Font = new Font(this.Font, FontStyle.Regular);
            }
            else
            {
                toolStripSearchBox.Text = "";
                toolStripSearchBox.ForeColor = SystemColors.ControlText;
                toolStripSearchBox.Font = new Font(this.Font, FontStyle.Regular);
            }
        }

        private void toolStripSearchBox_Leave(object sender, EventArgs e)
        {
            if (toolStripSearchBox.Text != String.Empty)
            {
                if (toolStripSearchBox.Text != toolStripSearchBox_standardText)
                {
                }
            }
            else
            {
                toolStripSearchBox.Text = toolStripSearchBox_standardText;
                toolStripSearchBox.ForeColor = SystemColors.ControlDark;
                toolStripSearchBox.Font = new Font(this.Font, FontStyle.Italic);
            }
        }

        #endregion

        // Runtime Layout
        private void Locations()
        {
            /*
            // Listview locations
            listView1.Location = new Point(3, (toolStripMain.Height + 3));
            listView2.Location = new Point((listView1.Location.X + listView1.Width + 3), (toolStripMain.Height + 3));

            // UserControl Location
            Point userControlLocation = new Point((listView2.Location.X + listView2.Width + 3), (listView2.Location.Y));
            
                       

            // formheight - toolstripheight - (2x3 margin)
            int formheight = this.ClientSize.Height - toolStripMain.Height - (2 * 3);

            // ListView height
            listView2.Height = formheight; 
            listView1.Height = formheight; 
            
            
            
            UCServerAttributes uc = new UCServerAttributes();
            uc.Location = userControlLocation;
            this.Controls.Add(uc);
             * */
        }

        // Resize Event
        private void Main_SizeChanged_1(object sender, EventArgs e)
        {
            Locations();
        }

        // Log Out
        private void toolStripLogoutButton_Click(object sender, EventArgs e)
        {
            // Show new login form
            Login login = new Login();
            this.Hide();
            login.ShowDialog();
            this.Close();
        }

        private void toolStripSearchButton_Click(object sender, EventArgs e)
        {
            findCI(toolStripSearchBox.Text);
        }

        void findCI(string text)
        {            
            // SQL query on toolStripSearchBox.Text in Table ci on ciNr and OudCI
            string sql = "SELECT * FROM ci WHERE ciNr LIKE '" + text + "' OR OudCI LIKE '" + text + "'";
            
        }
        private void lbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbCi.Items.Clear();
            changeCiCategory((CiCategory)lbCategories.SelectedItem);
        }

        public void changeCiCategory(CiCategory category)

        {
            switch (category)
            {
                case CiCategory.PC:
                    DataTable pcs = model.getPcs();
                    foreach (DataRow row in pcs.Rows)
                    {
                        lbCi.Items.Add(new CI(Convert.ToInt32(row["CInr"]), Convert.ToInt32(row["PcID"]), CiCategory.PC, row["typeNr"].ToString()));
                    }
                    break;

            }
        }

        private void lbCi_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeCi((CI)lbCi.SelectedItem);
        }

        public void changeCi(CI ci)
        {
            this.Controls.Remove(attributesPanel);
            attributesPanel = AttributePanelCreator.createPanel(model.getPc(ci.foreignId));
            this.Controls.Add(attributesPanel);
        }


    }
}
